package com.zcoj.controller.Admin.contest;

import com.zcoj.codeSimilar.service.CodeSimilarService;
import com.zcoj.codeSimilar.service.SubmitCodeContrastService;
import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.*;
import com.zcoj.pojo.Vo.ContentAnnouncementVO;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.*;
import com.zcoj.util.RedisCaches;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/19 08:35
 */
@RequestMapping("/adminContest")
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin','contest_admin')")
public class adminContestController {


    @Autowired
    private ContestService contestService;


    @Autowired
    private ContestProblemService contestProblemService;

    @Autowired
    private ProblemService problemService;


    @Autowired
    private RedisCaches redisCaches;

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private ContestAnnouncementService contestAnnouncementService;

    @Autowired
    private SubmitCodeContrastService submitCodeContrastService;

    @Autowired
    private CodeSimilarService codeSimilarService;



    @PostMapping("/createContest")
    public ApiResponse createContest(@RequestBody Contest contest){
        Integer integer = contestService.create(contest);
        if(Objects.nonNull(integer)&&integer!=0){
            return ApiResponse.success(integer);
        }else {
            return ApiResponse.failed("创建比赛失败");
        }
    }

    @PostMapping("/contestList")
    public ApiResponse contestList(@RequestBody PageWrap<Contest> pageWrap){
        return ApiResponse.success(contestService.findPage(pageWrap));
    }


    @GetMapping("/delContest/{id}")
    public ApiResponse delContest(@PathVariable Integer id){
        Integer integer = contestService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateContest")
    public ApiResponse updateContest(@RequestBody Contest contest){
        Integer updateById = contestService.updateById(contest);
        if(updateById!=0){
            return ApiResponse.success("更新成功",null);
        }else {
            return ApiResponse.failed("更新失败！！！");
        }
    }

    @GetMapping("/getContestById/{id}")
    public ApiResponse getContestById(@PathVariable Integer id){
        return ApiResponse.success( contestService.findById(id));
    }

    @GetMapping("/appendContestProblemData/{cid}")
    public ApiResponse appendContestProblemData(@PathVariable("cid") Integer cid,@RequestParam("pids") List<String> pids){
        Integer sum=0;
        for (String pid : pids) {
            ContestProblem contestProblem = new ContestProblem();
            contestProblem.setPid(Integer.parseInt(pid));
            Problem problem = problemService.findById(Integer.parseInt(pid));
            problem.setStatus(0);
            problemService.updateById(problem);
            contestProblem.setCid(cid);
            contestProblemService.create(contestProblem);
            sum++;
        }
        return ApiResponse.success(sum+"条题目数据添加完成",null);
    }
    //createContestAnnouncement

    @PostMapping("/createContestAnnouncement")
    public ApiResponse createContestAnnouncement(@RequestBody ContentAnnouncementVO contentAnnouncementVO){
        LoginUser loginUser = redisCaches.getLoginUser();
        Announcement announcement = new Announcement();
        announcement.setContent(contentAnnouncementVO.getContent());
        announcement.setTitle(contentAnnouncementVO.getTitle());
        announcement.setStatus(contentAnnouncementVO.getStatus());
        announcement.setUid(loginUser.getId());
        announcement.setAuthor(loginUser.getUsername());
        announcement.setCid(contentAnnouncementVO.getCid());
        if(Objects.nonNull(contentAnnouncementVO.getId())){
            announcement.setId(contentAnnouncementVO.getId());
            announcementService.updateById(announcement);
            return ApiResponse.success("公告更新成功");
        }else {
            Integer aid = announcementService.create(announcement);
            ContestAnnouncement contestAnnouncement = new ContestAnnouncement();
            contestAnnouncement.setAid(aid);
            contestAnnouncement.setCid(contentAnnouncementVO.getCid());
            Integer gAId = contestAnnouncementService.create(contestAnnouncement);
            if(aid!=0&&gAId!=0){
                return ApiResponse.success("公告添加成功");
            }else {
                return ApiResponse.failed("公告添加失败！！！");
            }
        }

    }



    @GetMapping("/selectedContestProblemList/{cid}")
    public ApiResponse selectedContestProblemList(@PathVariable Integer cid){
        ContestProblem contestProblem = new ContestProblem();
        contestProblem.setCid(cid);
        List<ContestProblem> list = contestProblemService.findList(contestProblem);
        List<Integer> problemIdList=new ArrayList<>();
        for (ContestProblem contestProblem1 : list) {
            problemIdList.add(contestProblem1.getPid());
        }
        return ApiResponse.success("",problemIdList);
    }

    @GetMapping("/editContestProblemData/{cid}")
    public ApiResponse editContestProblemData(@PathVariable("cid") Integer cid,@RequestParam("pids") List<String> pids){
        Integer sum=0;
        ContestProblem cp = new ContestProblem();
        cp.setCid(cid);
        List<ContestProblem> contestProblemServiceList = contestProblemService.findList(cp);
        for (ContestProblem contestProblem : contestProblemServiceList) {
           // list.add(contestProblem.getPid());
            contestProblemService.deleteById(contestProblem.getId());
        }
        for (String pid : pids) {
            ContestProblem contestProblem= new ContestProblem();
            contestProblem.setPid(Integer.parseInt(pid));
            contestProblem.setCid(cid);
            contestProblemService.create(contestProblem);
            sum++;
        }
        return ApiResponse.success(sum+"条题目数据更新完成",null);
    }

    @GetMapping("/getContestAnnouncement/{cid}")
    public ApiResponse getContestAnnouncement(@PathVariable Integer cid){

        ContestAnnouncement contestAnnouncement = new ContestAnnouncement();
        contestAnnouncement.setCid(cid);
        ContestAnnouncement contestAnnouncementServiceOne = contestAnnouncementService.findOne(contestAnnouncement);
        Announcement announcement=null;
        if( Objects.nonNull(contestAnnouncementServiceOne)){
            announcement = announcementService.findById(contestAnnouncementServiceOne.getAid());
        }

        return ApiResponse.success("",announcement);
    }

    @GetMapping("/submitContestCodeSimilarity/{cid}")
    public ApiResponse submitContestCodeSimilarity(@PathVariable Integer cid){
        Map<Integer, String> codeContrastResMap = submitCodeContrastService.canSubmitCodeContrast(cid);
        if(codeContrastResMap.containsKey(-1)){
            return ApiResponse.failed(codeContrastResMap.get(-1));
        }else {
            Collection<String> values = codeContrastResMap.values();
            return ApiResponse.success((String)values.toArray()[0],"");
        }
    }

    @GetMapping("/getContestIdAndTitle")
    public ApiResponse getContestIdAndTitle(){
        return ApiResponse.success(contestService.getContestIdAndTitle());
    }

    @GetMapping("/searchContestCodeSimilarity/{cid}")
    public ApiResponse searchContestCodeSimilarity(@PathVariable Integer cid){
        return ApiResponse.success(codeSimilarService.searchContestCodeSimilarity(cid));
    }


    @GetMapping("/deleteCodeSimilarRecord/{cid}")
    public ApiResponse deleteCodeSimilarRecord(@PathVariable Integer cid){
        String codeSimilarityCidKey="codeSimilarityCidKey::"+cid;
        Object cacheObject = redisCaches.getCacheObject(codeSimilarityCidKey);
        if(Objects.isNull(cacheObject)){
            codeSimilarService.deleteCodeSimilarRecord(cid);
            return ApiResponse.success("删除成功",null);
        }
        return ApiResponse.failed("该比赛正在进行查重，请勿执行删除操作！");
    }
}
